package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallAddressExample;
import com.cskaoyan.bean.CskaoyanmallSearchHistory;
import com.cskaoyan.bean.CskaoyanmallSearchHistoryExample;
import com.cskaoyan.mapper.CskaoyanmallSearchHistoryMapper;
import com.cskaoyan.service.HistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    CskaoyanmallSearchHistoryMapper cskaoyanmallSearchHistoryMapper;

    @Override
    public Map<String, Object> querySelective(Integer userId, String keyword, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        CskaoyanmallSearchHistoryExample searchHistoryExample = new CskaoyanmallSearchHistoryExample();
        CskaoyanmallSearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();

        if (!ObjectUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if (!ObjectUtils.isEmpty(keyword)){
            criteria.andKeywordEqualTo(keyword);
        }
        if (!ObjectUtils.isEmpty(sort)&&!ObjectUtils.isEmpty(order)){
            searchHistoryExample.setOrderByClause(sort+" "+order);
        }
        List<CskaoyanmallSearchHistory> searchHistories = cskaoyanmallSearchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<CskaoyanmallSearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("items",searchHistories);
        map.put("total",total);
        return  map;
    }
}
